// Copyright Epic Games, Inc. All Rights Reserved.

#include "LyraPlayerCameraManager.h"

#include "Async/TaskGraphInterfaces.h"
#include "Engine/Canvas.h"
#include "Engine/Engine.h"
#include "GameFramework/Pawn.h"
#include "GameFramework/PlayerController.h"
#include "LyraCameraComponent.h"
#include "LyraUICameraManagerComponent.h"

#include UE_INLINE_GENERATED_CPP_BY_NAME(LyraPlayerCameraManager)

class FDebugDisplayInfo;

static FName UICameraComponentName(TEXT("UICamera")); // UI相机组件名称

ALyraPlayerCameraManager::ALyraPlayerCameraManager(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer) // 调用父类构造函数
{
	DefaultFOV = LYRA_CAMERA_DEFAULT_FOV; // 设置默认视野
	ViewPitchMin = LYRA_CAMERA_DEFAULT_PITCH_MIN; // 设置默认最小俯仰角
	ViewPitchMax = LYRA_CAMERA_DEFAULT_PITCH_MAX; // 设置默认最大俯仰角

	UICamera = CreateDefaultSubobject<ULyraUICameraManagerComponent>(UICameraComponentName); // 创建默认子对象UI相机组件
}

ULyraUICameraManagerComponent* ALyraPlayerCameraManager::GetUICameraComponent() const
{
	return UICamera; // 返回UI相机组件
}

void ALyraPlayerCameraManager::UpdateViewTarget(FTViewTarget& OutVT, float DeltaTime)
{
	// 如果UI相机正在观察某物，让它具有优先权。
	if (UICamera->NeedsToUpdateViewTarget()) // 如果UI相机需要更新视角目标
	{
		Super::UpdateViewTarget(OutVT, DeltaTime); // 调用父类更新视角目标
		UICamera->UpdateViewTarget(OutVT, DeltaTime); // 更新UI相机视角目标
		return;
	}

	Super::UpdateViewTarget(OutVT, DeltaTime); // 调用父类更新视角目标
}

void ALyraPlayerCameraManager::DisplayDebug(UCanvas* Canvas, const FDebugDisplayInfo& DebugDisplay, float& YL, float& YPos)
{
	check(Canvas); // 检查Canvas有效

	FDisplayDebugManager& DisplayDebugManager = Canvas->DisplayDebugManager; // 获取调试显示管理器

	DisplayDebugManager.SetFont(GEngine->GetSmallFont()); // 设置字体
	DisplayDebugManager.SetDrawColor(FColor::Yellow); // 设置绘制颜色为黄色
	DisplayDebugManager.DrawString(FString::Printf(TEXT("LyraPlayerCameraManager: %s"), *GetNameSafe(this))); // 绘制相机管理器名称

	Super::DisplayDebug(Canvas, DebugDisplay, YL, YPos); // 调用父类显示调试

	const APawn* Pawn = (PCOwner ? PCOwner->GetPawn() : nullptr); // 获取Pawn

	if (const ULyraCameraComponent* CameraComponent = ULyraCameraComponent::FindCameraComponent(Pawn)) // 查找相机组件
	{
		CameraComponent->DrawDebug(Canvas); // 绘制相机组件调试信息
	}
}